package com.exercises;

import java.util.*;

class Exercise611 {
    public static void main(String[] args) {
        System.out.println(triangleNumber(new int[]{2, 2, 3, 4}));
    }

    public static int triangleNumber(int[] nums) {
        int result = 0;
        Arrays.sort(nums);
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            int a = nums[i];
            for (int j = i + 1; j < length; j++) {
                int b = nums[j];
                int k = j;
                while (k + 1 < nums.length && nums[k + 1] < a + b) {
                    k++;
                }
                result += Math.max(k - j, 0);
            }
        }
        return result;
    }
}
